package com.edu.study.base.StuCommonClassifyCatalog.service.impl;

import com.edu.study.base.StuCommonClassifyCatalog.domain.StuCommonClassifyCatalog;
import com.edu.study.base.StuCommonClassifyCatalog.mapper.StuCommonClassifyCatalogMapper;
import com.edu.study.base.StuCommonClassifyCatalog.service.IStuCommonClassifyCatalogService;
import com.edu.study.base.stuProductCatalog.domain.StuProductCatalog;
import com.edu.study.base.stuProductCatalog.service.IStuProductCatalogService;
import com.edu.study.base.stuQuestionLibrary.domain.StuQuestionLibraryRy;
import com.edu.study.base.stuQuestionLibrary.service.IStuQuestionLibraryRyService;
import com.edu.study.base.stuQuestionOption.service.IStuQuestionOptionService;
import com.edu.study.base.stuQuestionRelation.domain.StuQuestionRelationRy;
import com.edu.study.base.stuQuestionRelation.service.IStuQuestionRelationRyService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 产品组件目录Service业务层处理
 * 
 * @author zw
 * @date 2021-05-11
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuCommonClassifyCatalogServiceImpl implements IStuCommonClassifyCatalogService
{
    @Autowired
    private StuCommonClassifyCatalogMapper stuCommonClassifyCatalogMapper;

    @Autowired
    private IStuQuestionLibraryRyService stuQuestionLibraryRyService;

    @Autowired
    private IStuQuestionRelationRyService  stuQuestionRelationRyService;
    @Autowired
    private IStuQuestionOptionService stuQuestionOptionService;
    
    @Autowired
    private IStuProductCatalogService  stuProductCatalogService;

    /**
     * 查询产品组件目录
     * 
     * @param id 产品组件目录ID
     * @return 产品组件目录
     */
    @Override
    public StuCommonClassifyCatalog selectStuCommonClassifyCatalogById(String id)
    {
        return stuCommonClassifyCatalogMapper.selectStuCommonClassifyCatalogById(id);
    }

    /**
     * 查询产品组件目录列表
     * 
     * @param stuCommonClassifyCatalog 产品组件目录
     * @return 产品组件目录
     */
    @Override
    public List<StuCommonClassifyCatalog> selectStuCommonClassifyCatalogList(StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        return stuCommonClassifyCatalogMapper.selectStuCommonClassifyCatalogList(stuCommonClassifyCatalog);
    }



    @Override
    public List<StuCommonClassifyCatalog> buildClassifyCatalogTree(List<StuCommonClassifyCatalog> classifyCatalogList,StuCommonClassifyCatalog catalogParent) {
        List<StuCommonClassifyCatalog> returnList = new ArrayList<StuCommonClassifyCatalog>();
        List<StuCommonClassifyCatalog> rootList = new ArrayList<>();
        if(StringUtils.isNotEmpty(catalogParent.getCatalogParentId())){
            rootList = classifyCatalogList.stream().filter(c->c.getCatalogParentId().equals(catalogParent.getCatalogParentId())).collect(Collectors.toList());
        }else if(StringUtils.isNotEmpty(catalogParent.getBelongProductId())){
            rootList = classifyCatalogList.stream().filter(c->"0".equals(c.getCatalogParentId())).collect(Collectors.toList());
        }
        for (Iterator<StuCommonClassifyCatalog> iterator = rootList.iterator(); iterator.hasNext();)
        {
            StuCommonClassifyCatalog classifyCatalog = (StuCommonClassifyCatalog) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (hasChild(classifyCatalogList,classifyCatalog))
            {
                recursionFn(classifyCatalogList, classifyCatalog);
            }
            returnList.add(classifyCatalog);
        }
        if (returnList.isEmpty())
        {
            returnList = classifyCatalogList;
        }
        return returnList;
    }

    @Override
    public List<StuCommonClassifyCatalog> getCourseCatalogTree(List<StuCommonClassifyCatalog> catalogList) {
        List<String> catalogIdList = new ArrayList<String>();
        List<Long> questionLibraryIdList = new ArrayList<Long>();

        for (StuCommonClassifyCatalog catalog : catalogList) {
            catalogIdList.add(catalog.getId());
        }

        List<StuQuestionLibraryRy> stuQuestionLibraryList = stuQuestionLibraryRyService.selectCourseExercisesContentByIds(catalogIdList);
          //改成了数据库关联查询
        //        for (StuQuestionLibraryRy stuQuestionLibrary: stuQuestionLibraryList) {
//            questionLibraryIdList.add(stuQuestionLibrary.getId());
//        }
//
        //List<StuQuestionOption> stuQuestionOptions = stuQuestionOptionService.selectStuQuestionOptionListByIds(questionLibraryIdList);
//
//        for (StuQuestionLibraryRy stuQuestionLibrary: stuQuestionLibraryList) {
//            Long q_id = stuQuestionLibrary.getId();
//            if(stuQuestionOptions!=null && stuQuestionOptions.size()>0){
//                List<StuQuestionOption> optionList = stuQuestionOptions.stream().filter(o -> o.getQuestionLibraryId().equals(q_id)).collect(Collectors.toList());
//                stuQuestionLibrary.setOptionList(optionList);
//            }
//
//        }

        for (StuCommonClassifyCatalog catalog : catalogList) {
            String c_id = catalog.getId();
            if(stuQuestionLibraryList!=null && stuQuestionLibraryList.size()>0){
                List<StuQuestionLibraryRy> questionLibraryList = stuQuestionLibraryList.stream().filter(q -> q.getBussId().equals(c_id)).collect(Collectors.toList());
                catalog.setQuestions(questionLibraryList);
            }
        }

        return catalogList;
    }

    @Override
    public List<StuCommonClassifyCatalog> getClassifyCatalogTree(List<StuCommonClassifyCatalog> classifyCatalogList,StuCommonClassifyCatalog commonClassifyCatalog) {
        List<StuCommonClassifyCatalog> returnList = new ArrayList<StuCommonClassifyCatalog>();
        List<StuCommonClassifyCatalog> rootList = classifyCatalogList.stream().filter(c-> c.getCatalogParentId().equals(commonClassifyCatalog.getId())).collect(Collectors.toList());
        for(StuCommonClassifyCatalog catalog : rootList){
            String isCatalog = catalog.getIfOnlyCatalog();
            if("1".equals(isCatalog)){

            }else {

            }
        }

        return null;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<StuCommonClassifyCatalog> list, StuCommonClassifyCatalog t)
    {
        // 得到子节点列表
        List<StuCommonClassifyCatalog> childList = getChildList(list, t);
        t.setChildren(childList);
        for (StuCommonClassifyCatalog tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<StuCommonClassifyCatalog> getChildList(List<StuCommonClassifyCatalog> list, StuCommonClassifyCatalog t)
    {
        List<StuCommonClassifyCatalog> tlist = new ArrayList<StuCommonClassifyCatalog>();
        Iterator<StuCommonClassifyCatalog> it = list.iterator();
        while (it.hasNext())
        {
            StuCommonClassifyCatalog n = (StuCommonClassifyCatalog) it.next();
            if (StringUtils.isNotNull(n.getCatalogParentId()) && n.getCatalogParentId().equals(t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<StuCommonClassifyCatalog> list, StuCommonClassifyCatalog t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 新增产品组件目录
     * 
     * @param stuCommonClassifyCatalog 产品组件目录
     * @return 结果
     */
    @Override
    public int insertStuCommonClassifyCatalog(StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        return stuCommonClassifyCatalogMapper.insertStuCommonClassifyCatalog(stuCommonClassifyCatalog);
    }

    /**
     * 修改产品组件目录
     * 
     * @param stuCommonClassifyCatalog 产品组件目录
     * @return 结果
     */
    @Override
    public int updateStuCommonClassifyCatalog(StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        return stuCommonClassifyCatalogMapper.updateStuCommonClassifyCatalog(stuCommonClassifyCatalog);
    }

    /**
     * 批量删除产品组件目录
     * 
     * @param ids 需要删除的产品组件目录ID
     * @return 结果
     */
    @Override
    public int deleteStuCommonClassifyCatalogByIds(String[] ids)
    {
        return stuCommonClassifyCatalogMapper.deleteStuCommonClassifyCatalogByIds(ids);
    }

    /**
     * 删除产品组件目录信息
     * 
     * @param id 产品组件目录ID
     * @return 结果
     */
    @Override
    public int deleteStuCommonClassifyCatalogById(String id)
    {
        return stuCommonClassifyCatalogMapper.deleteStuCommonClassifyCatalogById(id);
    }

	@Override
	public StuCommonClassifyCatalog selectStuCommonClassifyCatalogByCourseId(String courseId) {
		StuProductCatalog  stuProductCatalog = stuProductCatalogService.selectStuProductCatalogById(courseId);
		StuCommonClassifyCatalog  sccc = new StuCommonClassifyCatalog();
		List<StuQuestionRelationRy> relationList = stuQuestionRelationRyService.selectStuQuestionRelationRyList(new StuQuestionRelationRy());
		// TODO  统计每一课程生词数量
		Map<String, Long> tradeAmountMap = relationList.stream().filter(o->o.getCourseId().equals(courseId))
				.collect(Collectors.groupingBy(StuQuestionRelationRy::getCourseId,Collectors.counting()));
		sccc.setWordNum(tradeAmountMap.get(courseId));
		sccc.setCatalogName(stuProductCatalog.getCourseName());
		sccc.setCatalogIcon(stuProductCatalog.getCoursePic()); 
		return sccc;
	}
}
